<?php

/**
 *   OPENSHOP     Supply Chain Management,SCM
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: fast_scm_order.php,v 1.0 2009/07/13 09:36:58 created $
 *   @author      FillBag <fillbag@hotmail.com>
 */


define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib_scm.php');

if($_REQUEST['act']=='list_goods'){
    /* 检查权限 */
    admin_priv();  
    	
    if(empty($_GET['id']))
    {
    	sys_msg('invalid param');
    }
    
    $id       = intval($_GET['id']);
    $supplier = get_supplier_info($id);
    if(empty($supplier))
    {
    	sys_msg('供应商不存在');
    }
       
    $smarty->assign('ur_here',       '供应商(' . $supplier['supplier_name'].')商品列表');    
	$smarty->assign('action_link',   array('text' => '供应商列表', 'href' => 'supplier_manage.php?act=list'));     
    $smarty->assign('full_page',     1);
 	$smarty->assign('form_action',   'insert_goods');    
    $smarty->assign('cat_list',      cat_list(0, $cat_id));
    $smarty->assign('brand_list',    get_brand_list());
      
    $smarty->assign('supplier',      $supplier);
    
    $list = get_supplier_goods_list();  
    $smarty->assign('supplier_goods_list', $list['operate_goods']);    
    $smarty->assign('filter',              $list['filter']);
    $smarty->assign('page_count',          $list['page_count']); 
    $smarty->assign('record_count',        $list['record_count']);
    
    assign_query_info();
    $smarty->display('scm_supplier_goods_list.tpl');
}

elseif($_REQUEST['act']=='query_goods'){
	
    $list = get_supplier_goods_list();
    $smarty->assign('supplier_goods_list',  $list['operate_goods']);
    $smarty->assign('filter',               $list['filter']);
    $smarty->assign('record_count',         $list['record_count']);
    $smarty->assign('page_count',           $list['page_count']);
    
    make_json_result($smarty->fetch('scm_supplier_goods_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));  
}

elseif($_REQUEST['act']=='insert_goods'){
    /* 检查权限 */
    admin_priv('add_scm_purchase_order');  
    
    if(empty($_REQUEST['id']) || !in_array($_REQUEST['type'], array('purchase', 'enter', 'return')))
    {
    	sys_msg('invalid param');
    }
    
    $supplier_id  = intval($_GET['id']);
    $order_type   = trim($_REQUEST['type']);
    $supplier     = get_supplier_info($supplier_id);
    if(empty($supplier))
    {
    	sys_msg('供应商不存在');
    }
	$order = array('order_sn'     => create_fast_order_sn($order_type),
	               'supplier_id'  => intval($supplier['supplier_id']),
	               'discount'     => floatval($supplier['supply_discount']),
	               'order_status' => 0,
	               'add_time'     => gmtime());
    $order_id = create_tmp_scm_order($order_type, $order);
    $order    = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在');
    }    
	edit_scm_order_goods($order_type, $order_id, $supplier_id, $_POST['goods_nums'], $supplier['supply_discount']);
	
    $order_stat = scm_order_amount($order_type, $order_id);    
    /* 更新商品总金额和订单总金额 */
    update_scm_order($order_type, $order_id, array('goods_number' => $order_stat['goods_number'], 
    										 	   'order_amount' => $order_stat['order_amount']));
    /* todo 记录日志 */
    $sn = $order['order_sn'];
    $sn .= ",总数量:$order_stat[goods_number],总金额:$order_stat[order_amount]";
    admin_log($sn, 'add', 'scm_'.$order_type.'_order');

    /* 跳回快速订单编辑 */
    os_header("Location: fast_scm_order.php?act=edit&type=$order_type&id=$order_id");
    exit;	
}

elseif($_REQUEST['act']=='edit'){
    /* 检查权限 */
    admin_priv('edit_scm_purchase_order');  
    if(empty($_GET['id']) || !in_array($_REQUEST['type'], array('purchase', 'enter', 'return')))
    {
    	sys_msg('invalid param');
    }
    
    $order_id     = intval($_GET['id']);
    $order_type   = trim($_REQUEST['type']);	
    $order        = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order) || ($order['order_status'] <> 0)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在或状态无效');
    }

    $smarty->assign('ur_here', '编辑'.fast_scm_order_ur($order_type));
    $smarty->assign('action_link', array('href' => 'scm.php?act=list_'.$order_type.'_order', 'text' => fast_scm_order_ur($order_type).'列表'));	  
           
	$smarty->assign('order', $order);   
	
    $goods_list = scm_order_goods($order_type, $order_id);
    $smarty->assign('goods_list', $goods_list);
        
    assign_query_info();
    $smarty->display('scm_fast_order_edit.tpl');
}

elseif($_REQUEST['act']=='update'){
    /* 检查权限 */
    admin_priv('edit_scm_purchase_order'); 
    	
    if(empty($_POST['id']) || !in_array($_REQUEST['type'], array('purchase', 'enter', 'return')))
    {
    	sys_msg('invalid param');
    }	
    $order_id     = intval($_POST['id']);
    $order_type   = trim($_POST['type']);
    $order        = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order) || ($order['order_status'] <> 0)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在或状态无效');
    }
        
       /* 更新商品总金额和订单总金额 */
    update_scm_order($order_type, $order_id, array('deliver_time' => local_strtotime($_POST['deliver_time']), 
    										 	   'order_brief'  => trim($_POST['order_brief'])));
    $links = array(
        array('href' => 'supplier_manage.php?act=list', 'text' => '返回供应商列表'),
        array('href' => 'scm.php?act=list_'.$order_type.'_order', 'text' => '返回'.fast_scm_order_ur($order_type).'列表'),
        array('href' => 'scm.php?act=info_'.$order_type.'_order&id='.$order_id, 'text' => '查看'.fast_scm_order_ur($order_type))
    );
    sys_msg('修改'.fast_scm_order_ur($order_type).'成功', 0, $links);    
}

elseif($_REQUEST['act']=='edit_post_goods'){
    if (empty($_POST['order_id']) || empty($_POST['goodslist']) || !in_array($_POST['type'], array('purchase', 'enter', 'return')))
    {
        sys_msg('invalid param');
    }	
    $order_id     = intval($_POST['order_id']);
    $order_type   = trim($_POST['type']);
    $order        = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order) || ($order['order_status'] <> 0)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在或状态无效');
    }
    
    $supplier_id = intval($_POST['supplier_id']);
    $discount    = floatval($_POST['discount']);
    $discount    = (!empty($discount) && $discount>0 && $discount<1)  ? floatval($_POST['discount']) : 1;	
    if(!is_array($_POST['goods_nums'])){
         sys_msg('你必须设置商品数量');   	
    }  
	edit_scm_order_goods($order_type, $order_id, $supplier_id, $_POST['goods_nums'], $discount);
	
    $order_stat = scm_order_amount($order_type, $order_id);
    /* 更新商品总金额和订单总金额 */
    update_scm_order($order_type, $order_id, array('goods_number' => $order_stat['goods_number'], 
    										 	   'order_amount' => $order_stat['order_amount']));
    /* todo 记录日志 */
    $sn = $order['order_sn'];
    $sn .= ",总数量:$order_stat[goods_number],总金额:$order_stat[order_amount]";
    admin_log($sn, 'edit', 'scm_'.$order_type.'_goods');

    /* 跳回订单商品 */
    os_header("Location: fast_scm_order.php?act=edit&type=$order_type&id=$order_id");
    exit;	
}

elseif($_REQUEST['act']=='update_post_goods'){
    if (empty($_POST['order_id']) || !in_array($_POST['type'], array('purchase', 'enter', 'return')))
    {
        sys_msg('invalid param');
    }
    $order_id     = intval($_POST['order_id']);
    $order_type   = trim($_POST['type']);
    $order        = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order) || ($order['order_status'] <> 0)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在或状态无效');
    }	
    update_scm_order_goods($order_type, $order_id, $_POST);  
    
    $order_stat = scm_order_amount($order_type, $order_id);
    /* 更新商品总金额和订单总金额 */
    update_scm_order($order_type, $order_id, array('goods_number' => $order_stat['goods_number'], 
    										 	   'order_amount' => $order_stat['order_amount']));
    /* todo 记录日志 */
    $sn = $order['order_sn'];
    $sn .= ",总数量:$order_stat[goods_number],总金额:$order_stat[order_amount]";
    admin_log($sn, 'edit', 'scm_'.$order_type.'_goods');

    /* 跳回订单商品 */
    os_header("Location: fast_scm_order.php?act=edit&type=$order_type&id=$order_id");
    exit;	   
}

elseif($_REQUEST['act']=='remove_goods'){
	if(empty($_GET['order_id']) || !in_array($_GET['type'], array('purchase', 'enter', 'return'))){
        sys_msg('invalid param');			
	}
    $order_id     = intval($_GET['order_id']);
    $order_type   = trim($_GET['type']);
    $order        = get_fast_scm_order_info($order_type, $order_id);
    if(empty($order) || ($order['order_status'] <> 0)){
    	sys_msg(fast_scm_order_ur($order_type).'不存在或状态无效');
    }  
	
	if(empty($_GET['goods_id']) && empty($_GET['rec_id'])){
        sys_msg('invalid param');		
	}	
	if(!empty($_GET['rec_id']) && $_GET['rec_id']>0){
		$db->query("DELETE FROM " . $os->table('scm_'.$order_type.'_goods') . " WHERE rec_id=" . intval($_GET['rec_id']));
	}else{
		$db->query("DELETE FROM " . $os->table('scm_'.$order_type.'_goods') . " WHERE order_id=$order_id AND goods_id=" . intval($_GET['goods_id']));
	}

    $order_stat = scm_order_amount($order_type, $order_id);
    /* 更新商品总金额和订单总金额 */
    update_scm_order($order_type, $order_id, array('goods_number' => $order_stat['goods_number'], 
    										 	   'order_amount' => $order_stat['order_amount']));
   	$sn = $order['order_sn'];
    admin_log($sn, 'remove', 'scm_'.$order_type.'_goods'); 										 
    /* 跳回订单商品 */
    os_header("Location: fast_scm_order.php?act=edit&type=$order_type&id=$order_id");
    exit;		
}
?>